package webBeans;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import dto.BookingDTO;
import dto.PackageDTO;
import beansInterfaces.BookingMgr;
import beansInterfaces.ConstraintsMgr;
import beansInterfaces.UserMgr;

@ManagedBean(name="bookingBean")
@SessionScoped
public class BookingBean implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	@EJB
	private BookingMgr bookingMgr;
	@EJB
	private UserMgr userMgr;
	@EJB
	private ConstraintsMgr c;
	
	
	
	private PackageDTO packageSelected;
	
	private PackageDTO packageOnEdit;
	
	@PostConstruct
	public void init()
	{	
		this.packageOnEdit=new PackageDTO();
		
		this.packageOnEdit.setDiscount(0);
		this.packageOnEdit.setAvailable(true);
	}
	
	
	public BookingBean(){}
	
	
	
	//Getters and Setters
	public BookingMgr getBookingMgr() {
		return bookingMgr;
	}

	public void setBookingMgr(BookingMgr bookingMgr) {
		this.bookingMgr = bookingMgr;
	}

	public UserMgr getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserMgr userMgr) {
		this.userMgr = userMgr;
	}

	public PackageDTO getPackageSelected() {
		return packageSelected;
	}

	public void setPackageSelected(PackageDTO packageSelected) {
		this.packageSelected = packageSelected;
	}
	
	public PackageDTO getPackageOnEdit() {
		return packageOnEdit;
	}
	
	public void setPackageOnEdit(PackageDTO packageOnEdit) {
		this.packageOnEdit = packageOnEdit;
	}
	
	public ConstraintsMgr getC() {
		return c;
	}


	public void setC(ConstraintsMgr c) {
		this.c = c;
	}
	


	
	
	//User Methods
	public void book(){
		
		if(this.packageOnEdit.getExcursion()==this.packageSelected.getExcursion()
			&& this.packageOnEdit.getResidence()==this.packageSelected.getResidence()
			&& this.packageOnEdit.getDepartureTransport()==this.packageSelected.getDepartureTransport()
			&& this.packageOnEdit.getReturnTransport()==this.packageSelected.getReturnTransport()
			
			&& this.c.isComplete(this.packageOnEdit))
		{
			BookingDTO booking = new BookingDTO();
			booking.setPackageChosen(packageSelected);
			
			String user=this.userMgr.getUserDTO().getUsername();
			booking.setUser(user);
			bookingMgr.bookPackage(booking);
		}
	}
	
	public void bookPersonalized(){
		if((this.packageOnEdit.getExcursion()!=this.packageSelected.getExcursion()
				|| this.packageOnEdit.getResidence()!=this.packageSelected.getResidence()
				|| this.packageOnEdit.getDepartureTransport()!=this.packageSelected.getDepartureTransport()
				|| this.packageOnEdit.getReturnTransport()!=this.packageSelected.getReturnTransport())
				
				&& this.c.isComplete(this.packageOnEdit))
			{
				BookingDTO booking = new BookingDTO();
				booking.setPackageChosen(packageOnEdit);
				String user=this.userMgr.getUserDTO().getUsername();
				booking.setUser(user);
				bookingMgr.bookPersonalizedPackage(booking);			
			}

		
	}
	
	public void bookInvitation(){
	BookingDTO booking= new BookingDTO();
	booking.setPackageChosen(packageSelected);
	booking.setUser(this.userMgr.getUserDTO().getUsername());
	
	
	bookingMgr.bookPackage(booking);
	}


	
}


